package com.ruoyi.quartz.task;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.ExtMonnifyOrder;
import com.ruoyi.bizsys.mapper.ExtMonnifyOrderMapper;
import com.ruoyi.business.component.RepaymentMqSendService;
import jodd.util.Base64;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 处理Monnify 问题bug的，手动触发
 *
 * @author ruoyi
 */
@Slf4j
@Component("dealMonnifyNotSuccessTask")
public class DealMonnifyNotSuccessTask {

    @Autowired
    RepaymentMqSendService repaymentMqService;
    @Autowired
    ExtMonnifyOrderMapper extMonnifyOrderMapper;

    /**
     * 需要 Base64decode后，在这个网站 https://base64.us/
     * sqlStr 参入进行执行
     */
    public void dealBySqlStr(String sqlStr) {
        sqlStr = Base64.decodeToString(sqlStr);
        log.info("处理Monnify 问题bug的，手动触发 Start>>>>>>>>>>,{}", sqlStr);
        List<ExtMonnifyOrder> list = extMonnifyOrderMapper.dealBySqlStr(sqlStr);
        if (!CollectionUtils.isEmpty(list)) {
            log.info("处理Monnify 问题bug的::开始处理：size:{}", list.size());
            for (int i = 0; i < list.size(); i++) {
                dealExtMonnifyOrder(list.get(i));
            }
        }

    }

    private void dealExtMonnifyOrder(ExtMonnifyOrder extMonnifyOrder) {
        JSONObject res = new JSONObject();
        res.put("extMonnifyOrder", extMonnifyOrder);
        res.put("custNo", extMonnifyOrder.getCustNo());
        repaymentMqService.syncSendMqRepayMentMonnify(res, extMonnifyOrder.getCustNo());

    }
}